package com.summarecon.database;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DatabaseDefectType {
	private static final String TABLE_DEFECT_TYPE = "jenis_defect";
	
	private SQLiteDatabase db;  
	private Context context = null;  
	private DatabaseHelper DBHelper;

	public DatabaseDefectType(Context ctx) 
	{
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}

	public void open() throws SQLException 
	{
		db = DBHelper.getWritableDatabase();
	}


	public void close() 
	{
		DBHelper.close();
	}

	public String[] getAllRowsAsArrays()
	{
		// create an ArrayList that will hold all the data collected from
		// the database.
		ArrayList<String> dataArrays = new ArrayList<String>();
		// This is a database call that creates a "cursor" object.
		// The cursor object store the information collected from the
		// database and is used to iterate through the data.
		Cursor cursor;

		try
		{
			// ask the database object to create the cursor.
			cursor = db.rawQuery("SELECT * FROM " + TABLE_DEFECT_TYPE, null);
			//db.rawQuery("SELECT * FROM " + TABLE_LOCATION + " WHERE cluster_id=?", new String[]{"2"});
			// move the cursors pointer to position zero.
			if (cursor != null) {           
				if(cursor.getCount() > 0)
				{
					if  (cursor.moveToFirst()) {
						do {
							dataArrays.add(cursor.getString(cursor.getColumnIndex("defect_name")));
						}while (cursor.moveToNext());
					}
				}
			}
			/*
            cursor.moveToFirst();

            // if there is data after the current cursor position, add it
            // to the ArrayList.
            if (!cursor.isAfterLast())
            {
                do
                {
                  dataArrays.add(cursor.getString(1) + cursor.getString(2));
                }
                // move the cursor's pointer up one position.
                while (cursor.moveToNext());
            }
			 */
		}
		catch (SQLException e)
		{
			Log.e("DB Error", e.toString());
			e.printStackTrace();
		}

		// return the ArrayList that holds the data collected from
		// the database.
		Log.i("data", dataArrays.toArray(new String[dataArrays.size()]).toString());
		return dataArrays.toArray(new String[dataArrays.size()]);
	}
}
